package com.dataseek.iot.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dataseek.iot.product.entity.PdtAttributeDic;
import com.dataseek.iot.product.entity.PdtFuncRel;
import com.dataseek.iot.product.mapper.PdtAttributeDicMapper;
import com.dataseek.iot.product.mapper.PdtFuncRelMapper;
import com.dataseek.iot.product.service.PdtFuncRelService;
import com.dataseek.iot.product.vo.PdtFuncRelVo;
import com.dataseek.iot.toolikit.common.enums.RspStatusEnum;
import com.dataseek.iot.toolikit.common.service.IDServiceHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @discription:
 * @author: Freya.Yan
 * @date: 2022/1/11
 */
@Service
@Slf4j
public class PdtFuncRelServiceImpl implements PdtFuncRelService {
    @Autowired
    private IDServiceHelper idServiceHelper;

    @Autowired
    private PdtFuncRelMapper pdtFuncRelMapper;

    @Autowired
    private PdtAttributeDicMapper pdtAttributeDicMapper;


    @Override
    public PdtFuncRelVo queryPdtFuncRel(String pdtCode, Integer num, Integer size) {
        List<PdtFuncRel> pdtFuncRelList = new ArrayList<>();
        PdtFuncRelVo pdtListVo = new PdtFuncRelVo();
        QueryWrapper<PdtFuncRel> qw = new QueryWrapper<>();
        if (pdtCode != null && !pdtCode.isEmpty()) {
            qw.like("pdt_code", pdtCode);
        }
        IPage<PdtFuncRel> page = new Page<>(num, size);
        List<PdtFuncRel> mapList = pdtFuncRelMapper.selectPage(page, qw).getRecords();
        for (int i = 0;i<mapList.size();i++) {
            PdtFuncRel pdtFuncRel = mapList.get(i);
            String attrCode = pdtFuncRel.getAttCode();
            QueryWrapper<PdtAttributeDic> qwAttr = new QueryWrapper<>();
            qwAttr.eq("att_code", attrCode);
            PdtAttributeDic pdtAttributeDic = pdtAttributeDicMapper.selectOne(qwAttr);
            pdtFuncRel.setAttName(pdtAttributeDic.getAttName());
            pdtFuncRel.setAttType(pdtAttributeDic.getAttType());
            pdtFuncRel.setDataType(pdtAttributeDic.getDataType());
            pdtFuncRelList.add(pdtFuncRel);
        }
        pdtListVo.setPage(num);
        pdtListVo.setSize(size);
        pdtListVo.setTotal(page.getTotal());
        pdtListVo.setList(pdtFuncRelList);
        return pdtListVo;
    }

    @Override
    public Integer insertPdtFuncRel(PdtFuncRel pdtFuncRel) {
        int resultCode = 0;
        long id = idServiceHelper.nextId();
        pdtFuncRel.setId(id);
        String code = pdtFuncRel.getAttCode();
        QueryWrapper<PdtFuncRel> qw = new QueryWrapper<>();
        qw.eq("att_code", code);
        Integer count = pdtFuncRelMapper.selectCount(qw);
        if (count == 0) {
           // pdtFuncibuteDic.setTenantCode(tenantCode);
            pdtFuncRelMapper.insert(pdtFuncRel);
        } else {
            resultCode = RspStatusEnum.FAIL.getCode();
        }
        return resultCode;

    }

    @Override
    public Integer updatePdtFuncRel(PdtFuncRel pdtFuncRel) {
        int resultCode = 0;
        long id = pdtFuncRel.getId();
        String attrCode = pdtFuncRel.getAttCode();
        String attrCodeOld = pdtFuncRelMapper.selectById(id).getAttCode();
        QueryWrapper<PdtFuncRel> qw = new QueryWrapper<>();
        qw.eq("att_code", attrCode);
        Integer count = pdtFuncRelMapper.selectCount(qw);
        if (count == 0) {
            resultCode = pdtFuncRelMapper.updateById(pdtFuncRel);
        } else if (count>0 && attrCodeOld.equals(attrCode)){
            resultCode = pdtFuncRelMapper.updateById(pdtFuncRel);
        }else {
            resultCode = RspStatusEnum.FAIL.getCode();
        }
        return resultCode;
    }

    @Override
    public Integer deletePdtFuncRel(List<Long> ids) {
        Integer resultCode = 0;
        resultCode = pdtFuncRelMapper.deleteBatchIds(ids);
        return resultCode;
    }

}
